package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.dtos.WmChannelDto;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.common.pojos.wemedia.WmChannel;
import com.heima.model.common.pojos.wemedia.WmNews;
import com.heima.wemedia.constant.WmConstants;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.mapper.WmNewsMapper;
import com.heima.wemedia.service.WmChannelService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author 陈辉
 * @data 2024 9:42
 */
@Service
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {
    @Autowired
    private WmNewsMapper wmNewsMapper;
    @Override
    public ResponseResult saveChannel(WmChannelDto channelDto) {
        if(channelDto == null) return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        WmChannel res = getOne(Wrappers.<WmChannel>lambdaQuery().eq(WmChannel::getName, channelDto.getName()));
        //频道名词不能重复
        if(res != null) return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        WmChannel channel = new WmChannel();
        BeanUtils.copyProperties(channelDto,channel);
        channel.setCreatedTime(new Date());
        if(channelDto.getIsDefault() == null) channel.setIsDefault(false);
        //前台输入内容进行频道的保存
        boolean save = save(channel);
        return ResponseResult.okResult(save?AppHttpCodeEnum.SUCCESS:AppHttpCodeEnum.SERVER_ERROR);
    }


    @Override
    public ResponseResult pageQuery(WmChannelDto channelDto) {
        // 设置默认分页参数
        if (channelDto.getPage() == null || channelDto.getSize() == null) {
            channelDto.setPage(WmConstants.DEFAULT_PAGE);
            channelDto.setSize(WmConstants.DEFAULT_SIZE);
        }

        // 创建查询条件包装器
        LambdaQueryWrapper<WmChannel> wrapper = Wrappers.lambdaQuery(WmChannel.class);
        if (StringUtils.isNotBlank(channelDto.getName())) {
            wrapper.like(WmChannel::getName, channelDto.getName());
        }
        if (channelDto.getStatus() != null) {
            wrapper.eq(WmChannel::getStatus, channelDto.getStatus());
        }
        wrapper.orderByDesc(WmChannel::getCreatedTime);

        // 创建分页对象
        Page<WmChannel> page = new Page<>(channelDto.getPage(), channelDto.getSize());

        page = this.page(page, wrapper);
        // 构造分页响应结果
        PageResponseResult prr = new PageResponseResult(
                (int) page.getCurrent(),
                (int) page.getSize(),
                (int) page.getTotal()
        );
        prr.setData(page.getRecords());
        return prr;
    }

    @Override
    public ResponseResult upate(WmChannelDto channelDto) {
        //参数校验
        if(channelDto == null || channelDto.getId() == null) return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        WmChannel channel = new WmChannel();
        BeanUtils.copyProperties(channelDto,channel);
        List<WmNews> wmNewsList = wmNewsMapper.selectList(Wrappers.<WmNews>lambdaQuery().eq(WmNews::getChannelId, channelDto.getId()));
        //如果频道被引|用则不能禁用
        if(wmNewsList != null && !wmNewsList.isEmpty()) channel.setStatus(null);
        updateById(channel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult deleteChannel(Integer id) {
        //参数校验
        if(id == null) return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        //只有禁用的频道才能删除
        WmChannel channel = getOne(Wrappers.<WmChannel>lambdaQuery().eq(WmChannel::getId, id));
        if(!channel.getStatus()) {
            removeById(id);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }else {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
    }

}
